Python simple way to format time [strftime function]

  • 2020-05-12 02:49:02
  • OfStack

This article illustrates the simple method of Python to format time, and shares it with you for your reference. The details are as follows:

walker often USES the current time and relative time to calculate the efficiency of program execution. It is convenient for copy to simply write down 1.


>>> import time
>>> startTime = time.time()
>>> '%.2fs' % (time.time() - startTime) # Relative time 
'24.51s'
>>> '{:.2f}s'.format(time.time() - startTime) # Relative time 
'37.55s'
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) # Formatting the current time 
'2016-01-05 10:34:06'

PS: this site also provides a tool about Unix timestamp conversion (summary of various common language timestamp operations), very practical, for your reference:

Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime

More about Python related content interested readers to view this site project: "skills summary Python date and time", "Python URL skills summary", "Python pictures skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article is helpful for you to design Python program.


Related articles: